package com.goktech.olala.client.controller.index;

import com.goktech.olala.core.req.ReqGoodKey;
import com.goktech.olala.core.resp.RespGoodsVo;
import com.goktech.olala.core.service.IGoodCategory;
import com.goktech.olala.core.service.IGoodIndex;
import com.goktech.olala.server.pojo.goods.Category;
import com.goktech.olala.server.pojo.goods.Good;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

/**
 * @Classname IndexGoodController
 * @Description TODO
 * @Date 2020/12/4 20:28
 * @Created by ASUS
 */
@RestController
@RequestMapping("/goodsIndexApi")
public class IndexGoodController {

    @Autowired
    IGoodIndex goodIndex;

    @Autowired
    IGoodCategory goodCategory;

    ModelAndView mav = new ModelAndView();

    @RequestMapping("/queryGoodsByParam.do")
    public ModelAndView queryGoodsByParam(HttpServletRequest request){
        //搜索关键字
        String key = request.getParameter("keywords");
        //从首页的商品搜索
        String categoryMidId = request.getParameter("categoryId");
        String keywords = null;
        if (key!=null){
            try {
                keywords = URLDecoder.decode(key,"utf-8");//转码成中文
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            ReqGoodKey goodKeys = new ReqGoodKey();
            goodKeys.setGoodKey(keywords);
            List<Good> goodsList = goodIndex.selectByKey(goodKeys);
            mav.addObject("goodsList",goodsList);
        }
       if (categoryMidId!=null){
           int categoryId = Integer.parseInt(categoryMidId);
           Category category = goodCategory.selectByPrimaryKey(categoryId);
           ReqGoodKey goodKeys = new ReqGoodKey();
           goodKeys.setGoodKey(category.getCategoryName());
           List<Good> goodsList = goodIndex.selectByKey(goodKeys);
           mav.addObject("categoryList",category);
           mav.addObject("goodsList",goodsList);
       }


        mav.setViewName("home/search");

        return mav;
    }

    //商品详细
    @RequestMapping("queryGoodsDetail.do")
    public ModelAndView queryGoodsDetail(HttpServletRequest request){
        String goodsId = request.getParameter("goodsId");

        RespGoodsVo goodsInfo = goodCategory.queryGoodsById(goodsId);

        mav.addObject("goodsInfo",goodsInfo);
        mav.setViewName("home/introduction");

        return mav;
    }

}
